package com.shhm.business.service.impl.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.shhm.business.dto.request.FilterProductReq;
import com.shhm.business.mapper.AnnouncementMapper;
import com.shhm.business.mapper.ProductMapper;
import com.shhm.business.mapper.ProductTypeMapper;
import com.shhm.business.service.business.BusinessHomeService;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.dto.security.SecurityUser;
import com.shhm.common.entity.admin.StudentEntity;
import com.shhm.common.entity.common.AnnouncementEntity;
import com.shhm.common.entity.common.ProductEntity;
import com.shhm.common.entity.common.ProductTypeEntity;
import com.shhm.common.utils.ResponseUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/9/10 下午12:46
 */
@Service
@RequiredArgsConstructor
public class BusinessHomeServiceImpl implements BusinessHomeService {
    private final ProductMapper productMapper;
    private final ProductTypeMapper productTypeMapper;
    private final AnnouncementMapper announcementMapper;

    @Override
    public AjaxResultPlus getHomeOverview() {
        Map<String, Object> resMap = new HashMap<>();
        AjaxResultPlus res = AjaxResultPlus.custom(200, false);
        List<ProductTypeEntity> prodTypeList = productTypeMapper.selectList(null);
        List<ProductEntity> productBannerList =productMapper.selectBannerList();
        LambdaQueryWrapper<AnnouncementEntity> viewAnn = new LambdaQueryWrapper<AnnouncementEntity>().eq(AnnouncementEntity::getDataStatus, 1);
        resMap.put("AllCategories", prodTypeList);
        resMap.put("productBannerList", productBannerList);
        resMap.put("announcementList", announcementMapper.selectList(viewAnn));
        return res.put("data", resMap);
    }

    @Override
    public AjaxResultPlus homeProductList(Integer pageNum, Integer pageSize) {
        AjaxResultPlus res = AjaxResultPlus.custom(200, false);
        SecurityUser user = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        PageDTO<ProductEntity> page = new PageDTO<>(pageNum, pageSize);
        return res.put("data",productMapper.haveUniversityList(new PageDTO<>(pageNum, pageSize),user.getUniversityCode()));
    }

    @Override
    public AjaxResultPlus productTypeList(Integer pageNum, Integer pageSize, FilterProductReq entity) {
        PageDTO<ProductEntity> res = productMapper.filterProductsParams(new PageDTO<>(pageNum, pageSize), entity);
        return AjaxResultPlus.success( res,false);
    }

    @Override
    public AjaxResultPlus applicationStudent(StudentEntity entity) {
        entity.createLocalDateTime();
        return ResponseUtil.sqlExeIsSuccess(productMapper.applicationStudent(entity),"请确认填写的数据是否存在或者已填写!");
    }
}
